﻿using Aker.DevTeam;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using Extensions;
using System;
using System.Data;
using System.Windows.Forms;

namespace Aker
{
    public partial class Shelf : XForm
    {
        public Shelf()
        {
            InitializeComponent();
        }

        private void Shelf_Load(object sender, EventArgs e)
        {

            Data.TAM.ShelfTableAdapter.Fill(Data.DB.Shelf);
            Data.TAM.DepoTableAdapter.Fill(Data.DB.Depo);
            gv.Init(Data.DB.Shelf);
            gv.MakeEditable("ShelfId");
            gv.Columns["DepoId"].ColumnEdit = Utility.GetAutoCompGLE(Data.DB.Depo, "Depo", "DepoId");
            //gv.Columns["YetkiId"].ColumnEdit = GetYetkiGLE();
            //gv.SetCaptions("YetkiId:Yetki");
            //gv.MakeColumnInvisible("Yonetici");
            gv.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            gv.OptionsView.ShowViewCaption = false;

            gv.CellValueChanged += gv_CellValueChanged;

            GridMenu gm = GridManager.GetManager(gv).GridMenu;
            gm.AddMenu(Language.Stock.PrintShelfLabels, PrintLabels);
            gm.AddMenu("Büyük Raf Etiketi Yazdır", PrintLabelsBig);

            if (AppSession.Language == "TR")
                gv.SetCaptions("ShelfId:#", "Name:Raf", "Description:Açıklama");
            else
                gv.SetCaptions("ShelfId:#");

            this.Text = Language.Stock.Shelfs;

            gv.BestFitColumns();

        }

        private void PrintLabelsBig(object sender, EventArgs e)
        {
            EtiketYazdir(2);
        }

        private void PrintLabels(object sender, EventArgs e)
        {
            EtiketYazdir(1);
        }

        private void EtiketYazdir(int type)
        {
            gv.EndEditing();

            int[] rowHandles = gv.GetSelectedRows();
            DataTable dtShelf = Data.DB.Shelf.Clone();

            if (rowHandles.Length == 0)
            {
                XtraMessageBox.Show(Language.Product.NoSelection, Language.Uyari, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            foreach (int rowHandle in rowHandles)
            {
                dtShelf.ImportRow(gv.GetDataRow(rowHandle));
            }

            ProductReportSettings settings = new ProductReportSettings();
            settings.chkManualSirali.Checked = false;
            settings.chkManualSirali.Visible = false;
            settings.lblManual.Visible = false;


            if (settings.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int emptyCount = Convert.ToInt32(settings.emptyCount.Value);
                int copyCount = Convert.ToInt32(settings.copyCount.Value);
                if (type == 1)
                {
                    ShelfReport sr = new ShelfReport(dtShelf, emptyCount, copyCount);
                    sr.ShowPreview();
                }
                else if (type == 2)
                {
                    ShelfReportBilecik sr = new ShelfReportBilecik(dtShelf, emptyCount, copyCount);
                    sr.ShowPreview();
                }
            }
        }

        private void gv_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            gv.EndEditing();
            Data.TAM.ShelfTableAdapter.Update(Data.DB.Shelf);
        }
    }
}
